﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BoutiquePOS.DAL.DAO;
using BoutiquePOS.DAL.Gateway;
using BoutiquePOS.Manager;

namespace BoutiquePOS.UI.UserUI
{
    public partial class CustomerPaymentEntry : System.Web.UI.Page
    {
        private CustomerPaymentManager aManager = new CustomerPaymentManager();
        private CustomerPayment aPayment = new CustomerPayment();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    string dateAsString = DateTime.Now.ToString("yyyy-MM-dd");
                    paymentDateTextBox.Value = dateAsString;
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

        protected void SaveButton(object sender, EventArgs e)
        {
            try
            {
                SalesGateway salesGateway = new SalesGateway();
                int id = salesGateway.ReturnSalesId(salesIdTextBox.Text);

                aManager = new CustomerPaymentManager();
                Sales aSales = new Sales();
                aSales.SalesId = id;
                aPayment.ASales = aSales;

                aPayment.PaymentDate = paymentDateTextBox.Value;
                aPayment.PaidAmount = Convert.ToDecimal(paidAmountTextBox.Value);
                string message = aManager.SaveCustomerPayment(aPayment);
                messageResult.InnerText = message;

                Clear();
            }
            catch (Exception exception)
            {
                messageResult.InnerText = "Customer payment can not be saved" + "\n" + exception.ToString();
            }
        }

        protected void CloseButton(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
        protected void ClearButton(object sender, EventArgs e)
        {
            Clear();
        }
        private void Clear()
        {
            salesIdTextBox.Text = "";
            dueAmountTextBox.Value = "";
            paymentDateTextBox.Value = "";
            paidAmountTextBox.Value = "";
        }

        protected void DeleteButton(object sender, EventArgs e)
        {
            try
            {
                int customerPaymentId = Convert.ToInt32(customerPaymentIdTextBox.Value);
                CustomerPaymentManager customerPaymentManager = new CustomerPaymentManager();
                string aa = customerPaymentManager.DeleteCustomerPayment(customerPaymentId);
                messageResult.InnerText = aa;
            }
            catch (Exception)
            {
                messageResult.InnerText = "Delete Failed";
            }
        }

        protected void salesIdTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                SalesGateway salesGateway = new SalesGateway();
                int id = salesGateway.ReturnSalesId(salesIdTextBox.Text);
                CustomerPaymentGateway aCustomerPaymentGateway = new CustomerPaymentGateway();
                decimal totaldue = aCustomerPaymentGateway.GetDuesAmount(id);
                dueAmountTextBox.Value = totaldue.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}